/**
 * Created by zhangxiaoyu on 16/12/07.
 */
angular.module('hyt.myOrderCtrl',[])

  .controller('OrderListCtrl',function($scope,$filter,$ionicModal,Orders,Storage,$stateParams,OperationOrder,$ionicLoading,$ionicPopup,$state){
    $scope.showloading=true;
    $scope.htmlId=$stateParams.htmlId;
    var loginMsg=Storage.get('user');
    $scope.orderSearch={
      parameter:{
        ORDER_NO:'',
        ORDER_TYPE:'',
        ORDER_STATUS:'',
        CREATETIME_START:'',
        CREATETIME_END:''
      },
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    Orders.getOrderData($scope.orderSearch);
    $scope.$on('OrderList.Updated', function() {
      $scope.orderData=Orders.getResult();
      $scope.showloading=false;
    });
    //下拉刷新
    $scope.doRefresh=function(){
      Orders.getOrderData($scope.orderSearch);
      $scope.$broadcast('scroll.refreshComplete');
    };
    //上拉加载更多
    $scope.loadMore=function(){
      Orders.getMoreOrderData($scope.orderSearch);
      $scope.$broadcast('scroll.infiniteScrollComplete');
    };
    //下一页
    $scope.hasNextPage = function() {
      return Orders.hasNextPage();
    };
    var t = new Date();
    var iToDay=t.getDate();
    var iToMon=t.getMonth();
    var iToYear=t.getFullYear();
    var iTDate=new Date(iToYear,iToMon,(iToDay-30));
    $scope.startDate=$filter('date')(iTDate,'yyyy-MM-dd');
    $scope.showStartDate = function() { //选择日期
      var optionsStart = {
        date: iTDate,
        mode: 'date',
        maxDate: new Date($scope.endDate) - 10000
      };
      datePicker.show(optionsStart, function(d) {
        if (!isNaN(d.getTime())) {  // valid date
          $scope.$apply(function () {
            iTDate=d;
            $scope.startDate = $filter('date')(iTDate,'yyyy-MM-dd');
          });
        }
      });
    };
    $scope.endDate=$filter('date')(new Date(),'yyyy-MM-dd');
    $scope.showEndDate = function(){
      console.log('EndDate');
      var optionsEnd = {
        date : new Date(),
        mode : 'date',
        minDate: new Date($scope.startDate) - 10000
      };
      datePicker.show(optionsEnd,function(d) {
        if (!isNaN(d.getTime())) {  // valid date
          $scope.$apply(function () {
            endTime=d;
            $scope.endDate = $filter('date')(endTime,'yyyy-MM-dd');
          });
        }
      });
    };
    $scope.searchOrder=function(){
      $scope.orderSearch.parameter.CREATETIME_START=$scope.startDate;
      $scope.orderSearch.parameter.CREATETIME_END=$scope.endDate;
      $scope.orderSearch.parameter.ORDER_STATUS=$('#orderState').val();
      $scope.orderSearch.parameter.ORDER_TYPE=$('#orderType').val();
      Orders.getOrderData($scope.orderSearch);
      $scope.modal.hide();
    };
    $scope.$on('$destroy', function() {
      $scope.modal.remove();
    });
    $ionicModal.fromTemplateUrl('templates/tab-workbench/myOrder/searchOrder.html', {
      scope: $scope
    }).then(function(modal) {
      $scope.modal = modal;
    });
    $scope.operationOrder=function(id,status){
      var orderOperation={
        ID:id,
        ORDER_STATUS:status,
        userMsg:{
          username:loginMsg.username,
          password:loginMsg.password,
          uuid:loginMsg.uuid
        }
      };
      if (status==1){
        var receipt = $ionicPopup.confirm({
          title : '是否受理该订单',
          cancelText : '否',
          cancelType : 'button-stable',
          okText : '是'
        });
        receipt.then(function(r){
          if(r){
            OperationOrder.getOperationOrder(JSON.stringify(orderOperation));
          }else{
            console.log('取消');
          }
        });
      }else if (status==2){
        var receipt = $ionicPopup.confirm({
          title : '是否拒绝该订单',
          cancelText : '否',
          cancelType : 'button-stable',
          okText : '是'
        });
        receipt.then(function(r){
          if(r){
            OperationOrder.getOperationOrder(JSON.stringify(orderOperation));
          }else{
            console.log('取消');
          }
        });
      }
      $scope.$on('OperationOrder.Updated', function() {
        var result=OperationOrder.getResult();
        if (result.success==true){
          $ionicLoading.show({
            noBackdrop: true,
            template: result.msg,
            duration: 1500
          });
          Orders.getOrderData($scope.orderSearch);
        }
      });
    };
    $scope.singleTurn=function(id,type){
      if (type==2){
        $state.go('transWaybill',{id:id});
      }else {
        $state.go('addWaybill',{orderId:id});
      }
    };
    $scope.goOrderDetail=function (data,type) {
      Storage.set('orderDetail',data);
      if (type=='2'){
        $state.go('transOrderDetail',{id:data.ID});
      }else {
        $state.go('orderDetail');
      }
    }
  })
  .controller('OrderDetailCtrl',function($scope,Storage,$state) {
    console.log(Storage.get('orderDetail'));
    $scope.orderDetail=Storage.get('orderDetail');
    switch ($scope.orderDetail.ORDER_STATUS){
      case '0' :
        $scope.orderStatus = '待受理';
        break;
      case '1' :
        $scope.orderStatus = '已受理';
        break;
      case '2' :
        $scope.orderStatus = '已拒绝';
        break;
      case '3' :
        $scope.orderStatus = '已承运';
        break;
      case '4' :
        $scope.orderStatus = '已作废';
        break;
      default :
        break;
    }
    switch ($scope.orderDetail.ORDER_TYPE){
      case '0' :
        $scope.orderType = 'APP订单';
        break;
      case '1' :
        $scope.orderType = '客服订单';
        break;
      case '2' :
        $scope.orderType = '中转订单';
        break;
      case '3' :
        $scope.orderType = '网络订单';
        break;
      default :
        break;
    }
    switch ($scope.orderDetail.PAYMENT_SWITCH){
      case '0' :
        $scope.paymentSwitch = '现付';
        break;
      case '1' :
        $scope.paymentSwitch = '到付';
        break;
      case '2' :
        $scope.paymentSwitch = '回付';
        break;
      case '3' :
        $scope.paymentSwitch = '月结';
        break;
      case '4' :
        $scope.paymentSwitch = '货到打卡';
        break;
      case '5' :
        $scope.paymentSwitch = '欠付';
        break;
      case '6' :
        $scope.paymentSwitch = '代扣';
        break;
      case '7' :
        $scope.paymentSwitch = '多笔付';
        break;
      default :
        break;
    }
    switch ($scope.orderDetail.PICKUP_SWITCH){
      case '0' :
        $scope.pickupSwitch = '是';
        break;
      case '1' :
        $scope.pickupSwitch = '否';
        break;
      default :
        break;
    }
    switch ($scope.orderDetail.INSURE_SWITCH){
      case '0' :
        $scope.insureSwitch = '是';
        break;
      case '1' :
        $scope.insureSwitch = '否';
        break;
      default :
        break;
    }
    switch ($scope.orderDetail.RECEIPT_SWITCH){
      case '0' :
        $scope.receiptSwitch = '是';
        break;
      case '1' :
        $scope.receiptSwitch = '否';
        break;
      default :
        break;
    }
  })
  .controller('TransWaybillCtrl',function($scope,Storage,$stateParams,$state,WaybillsOfTrans,$ionicLoading) {
    $scope.showloading = true;
    var loginMsg = Storage.get('user');
    $scope.transWaybillSearch = {
      ID:$stateParams.id,
      userMsg: {
        username: loginMsg.username,
        password: loginMsg.password,
        uuid: loginMsg.uuid
      }
    };
    WaybillsOfTrans.getWaybillsOfTrans($scope.transWaybillSearch);
    $scope.$on('WaybillsOfTrans.Updated', function () {
      $scope.waybillsOfTransData = WaybillsOfTrans.getResult();
      $scope.showloading = false;
    });
    //下拉刷新
    $scope.doRefresh = function () {
      WaybillsOfTrans.getWaybillsOfTrans($scope.transWaybillSearch);
      $scope.$broadcast('scroll.refreshComplete');
    };
    //上拉加载更多
    $scope.loadMore = function () {
      WaybillsOfTrans.getMoreWaybillsOfTransData($scope.transWaybillSearch);
      $scope.$broadcast('scroll.infiniteScrollComplete');
    };
    //下一页
    $scope.hasNextPage = function () {
      return WaybillsOfTrans.hasNextPage();
    };
    $scope.goAddTransWb=function(LWID,TRANSFER_STATUS){
      if (TRANSFER_STATUS==1){
        $ionicLoading.show({
          noBackdrop: true,
          template: "该单已承运,请选择其它单据",
          duration: 1500
        });
      }else if (TRANSFER_STATUS==0){
        $state.go('addTransWaybill',{LWID:LWID});
      }
    }
  })
  .controller('AddTransWaybillCtrl',function($scope,$filter,$ionicModal,$stateParams,Storage,$ionicPopup,Sites,$ionicLoading,$timeout,$state,PackType,$cordovaHdoitBle,getPrinter,$ionicPopover,WayDetailOfTrans,AddTransWaybill,UserByDept){
    var loginMsg=Storage.get('user');
    var userMsg=Storage.get('loginResult');
    var wayDetailOfTrans={
      ID:$stateParams.LWID,
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    $scope.WAYBILL_DATE=$filter('date')(new Date(),'yyyy-MM-dd');
    $scope.REMARK='';
    WayDetailOfTrans.getWayDetailOfTrans(JSON.stringify(wayDetailOfTrans));
    $scope.$on('WayDetailOfTrans.Updated', function() {
      var result=WayDetailOfTrans.getResult();
      if (result.success==true){
        $scope.transWaybill=result.rows;
        switch ($scope.transWaybill.waybill.DELIVERY_TYPE){
          case '0' :
            $scope.DELIVERY_TYPE = '自提';
            break;
          case '1' :
            $scope.DELIVERY_TYPE = '送货';
            break;
          case '2' :
            $scope.DELIVERY_TYPE = '上楼';
            break;
          default :
            break;
        }
        //switch ($scope.transWaybill.paymentMap.PAYMENT_METHOD){
        //  case '0' :
        //    $scope.PAYMENT_METHOD = '现付';
        //    break;
        //  case '1' :
        //    $scope.PAYMENT_METHOD = '到付';
        //    break;
        //  case '2' :
        //    $scope.PAYMENT_METHOD = '欠付';
        //    break;
        //  case '3' :
        //    $scope.PAYMENT_METHOD = '回付';
        //    break;
        //  case '4' :
        //    $scope.PAYMENT_METHOD = '月结';
        //    break;
        //  case '5' :
        //    $scope.PAYMENT_METHOD = '货到打卡';
        //    break;
        //  case '6' :
        //    $scope.PAYMENT_METHOD = '代扣';
        //    break;
        //  case '7' :
        //    $scope.PAYMENT_METHOD = '多笔付';
        //    break;
        //  default :
        //    break;
        //}
      }
    });
    //选择承运日期
    $scope.showDate = function() { //选择日期
      var optionsStart = {
        date:new Date(),
        mode: 'date'
      };
      datePicker.show(optionsStart, function(d) {
        if (!isNaN(d.getTime())) {  // valid date
          $scope.$apply(function () {
            $scope.WAYBILL_DATE = $filter('date')(d,'yyyy-MM-dd');
          });
        }
      });
    };
    //包装方式
    var packType={
      type:'pack_type',
      username:loginMsg.username,
      password:loginMsg.password,
      uuid:loginMsg.uuid
    };
    PackType.getPackType(JSON.stringify(packType));
    $scope.$on('PackType.Updated', function() {
      var result=PackType.getResult();
      if (result.success==true){
        $scope.packTypes=result.rows;
      }
    });
    //到达站
    var sites={
      companyId:userMsg.rows.companyId,
      username:loginMsg.username,
      password:loginMsg.password,
      uuid:loginMsg.uuid
    };
    Sites.getSites(JSON.stringify(sites));
    $scope.$on('Sites.Updated', function() {
      var result=Sites.getResult();
      if (result.success==true){
        $scope.sites=result.rows;
      }
    });
    //业务员
    var userByDept={
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    UserByDept.getUserByDept(JSON.stringify(userByDept));
    $scope.$on('UserByDept.Updated', function() {
      var result=UserByDept.getResult();
      if (result.success==true){
        $scope.userDepts=result.rows;
      }
    });
    //保存承运单
    var isButton;
    function  addWaybill(){
      var waybillTr={
        info:{
          BELONG_ID:userMsg.rows.baseDeptId, //所属站点，网点
          TRANS_ID:$stateParams.LWID,
          WAYBILL_DATE:$scope.WAYBILL_DATE, //承运日期
          DEPARTURE_STATION:userMsg.rows.deptName, //始发站
          DESTINATION_ID:'', //到达站
          DESTINATION_STATION:$scope.transWaybill.waybill.DESTINATION_STATION, //目的站（名称）
          DELIVERY_TYPE:$scope.transWaybill.waybill.DELIVERY_TYPE, //送货方式（0-自提；1-上楼；2-送货）
          WAYBILL_TYPE:'', //运单类型(0-自营；1-联运；2-中转)
          RECEIPT_NUM:$scope.transWaybill.waybill.RECEIPT_NUM, //回单需要签回的份数
          INSURE_FLAG:'',//是否投保(0是；1否)
          userId:'', //业务员
          REMARK:$scope.REMARK //备注
        },
        customerMap:{
          S_CUSTOMER_NAME:$scope.transWaybill.customers.S_CUSTOMER_NAME, //发货人名称
          S_CUSTOMER_PHONE:$scope.transWaybill.customers.S_CUSTOMER_PHONE, //发货人电话
          S_CUSTOMER_ADDRESS:$scope.transWaybill.customers.S_CUSTOMER_ADDRESS, //发货人地址
          R_CUSTOMER_NAME:$scope.transWaybill.customers.R_CUSTOMER_NAME, //收货人名称
          R_CUSTOMER_PHONE:$scope.transWaybill.customers.R_CUSTOMER_PHONE, //收货人电话
          R_CUSTOMER_ADDRESS:$scope.transWaybill.customers.R_CUSTOMER_ADDRESS //收货人地址
        },
        costMap:{
          DELIVERY_FEE:$scope.transWaybill.costMap.DELIVERY_FEE, //提货费
          DECLARE_VALUE:$scope.transWaybill.costMap.DECLARE_VALUE, //声明价值
          PREMIUM:$scope.transWaybill.costMap.PREMIUM, //保费
          PAYMENT_FREIGHT:$scope.transWaybill.costMap.PAYMENT_FREIGHT, //运费垫付
          DELIVERY_COST:$scope.transWaybill.costMap.DELIVERY_COST, //送货费
          AGENCY_FUND:$scope.transWaybill.costMap.AGENCY_FUND, //代收款
          PAY_COLLECTION:$scope.transWaybill.costMap.PAY_COLLECTION, //代收款垫付
          FREIGHT_AMOUNT:$scope.transWaybill.costMap.FREIGHT_AMOUNT //运费合计
        },
        payMap:{
          SPOT_PAYMENT:$scope.transWaybill.paymentMap.SPOT_PAYMENT, //现付
          TO_PAYMENT:$scope.transWaybill.paymentMap.TO_PAYMENT, //到付
          BACK_PAY:$scope.transWaybill.paymentMap.BACK_PAY, //欠付
          BACK_TO_PAY:$scope.transWaybill.paymentMap.BACK_TO_PAY, //回付
          MONTHLY_BALANCE:$scope.transWaybill.paymentMap.MONTHLY_BALANCE, //月结
          GOODS_TO_CLOCK:$scope.transWaybill.paymentMap.GOODS_TO_CLOCK, //货到打卡
          WITHHOLD:$scope.transWaybill.paymentMap.WITHHOLD, //代扣
          BACK_NOW:$scope.transWaybill.paymentMap.BACK_NOW, //现返
          OWE_TO_RETURN:$scope.transWaybill.paymentMap.OWE_TO_RETURN, //欠返
          PAYMENT_METHOD:'7' //付款方式 0-现付；1-到付；2-欠付；3-回付；4-月结；5-货到打卡；6-代扣；7-多笔付(1至6的付款方式均有值)
        },
        goodsCollection:[{
          GOODS_NAME:$scope.transWaybill.goods[0].GOODS_NAME, //货物名称
          PIECES:$scope.transWaybill.goods[0].PIECES, //件数
          WEIGHT:$scope.transWaybill.goods[0].WEIGHT, //重量
          VOLUME:$scope.transWaybill.goods[0].VOLUME, //体积
          GOODS_PACK:$scope.transWaybill.goods[0].GOODS_PACK //包装
        }],
        userMsg:{
          username:loginMsg.username,
          password:loginMsg.password,
          uuid:loginMsg.uuid
        }
      };
      waybillTr.info.DESTINATION_ID=$("#site option:selected").val();
      waybillTr.info.userId=$("#userDept option:selected").val();
      //判断运单类型
      if (waybillTr.info.DESTINATION_ID!=''&&waybillTr.info.DESTINATION_STATION==''){
        waybillTr.info.WAYBILL_TYPE='0';
      }else if (waybillTr.info.DESTINATION_ID!=''&&waybillTr.info.DESTINATION_STATION!=''){
        waybillTr.info.WAYBILL_TYPE='1';
      }else if (waybillTr.info.DESTINATION_ID==''&&waybillTr.info.DESTINATION_STATION!=''){
        waybillTr.info.WAYBILL_TYPE='2';
      }
      if (waybillTr.payMap.SPOT_PAYMENT==''||waybillTr.payMap.SPOT_PAYMENT==undefined){
        waybillTr.payMap.SPOT_PAYMENT=0;
      }
      if (waybillTr.payMap.TO_PAYMENT==''||waybillTr.payMap.TO_PAYMENT==undefined){
        waybillTr.payMap.TO_PAYMENT=0;
      }
      if (waybillTr.payMap.BACK_TO_PAY==''||waybillTr.payMap.BACK_TO_PAY==undefined){
        waybillTr.payMap.BACK_TO_PAY=0;
      }
      if (waybillTr.payMap.MONTHLY_BALANCE==''||waybillTr.payMap.MONTHLY_BALANCE==undefined){
        waybillTr.payMap.MONTHLY_BALANCE=0;
      }
      if (waybillTr.payMap.GOODS_TO_CLOCK==''||waybillTr.payMap.GOODS_TO_CLOCK==undefined){
        waybillTr.payMap.GOODS_TO_CLOCK=0;
      }
      if (waybillTr.payMap.BACK_PAY==''||waybillTr.payMap.BACK_PAY==undefined){
        waybillTr.payMap.BACK_PAY=0;
      }
      if (waybillTr.payMap.WITHHOLD==''||waybillTr.payMap.WITHHOLD==undefined){
        waybillTr.payMap.WITHHOLD=0;
      }
      if (waybillTr.costMap.DECLARE_VALUE!=''){
        waybillTr.info.INSURE_FLAG='0';
      }else {
        waybillTr.info.INSURE_FLAG='1';
      }
      if(waybillTr.info.DESTINATION_ID==''&&waybillTr.info.DESTINATION_STATION==null) {
        $ionicLoading.show({
          noBackdrop: true,
          template: "请选择到达站或输入目的地",
          duration: 1500
        });
        $timeout(function(){
          $("#site").focus();
        },160);
      }else if (waybillTr.info.userId==''||waybillTr.info.userId==null){
        $ionicLoading.show({
          noBackdrop: true,
          template: "请选择业务员",
          duration: 1500
        });
        $timeout(function(){
          $("#userDept").focus();
        },160);
      }else {
        var confirmPopup = $ionicPopup.confirm({
          title: "运费合计为:"+waybillTr.costMap.FREIGHT_AMOUNT+'元',
          okText:'确定',
          cancelText:'取消'
        });
        confirmPopup.then(function(res) {
          if(res) {
            console.log(waybillTr);
            AddTransWaybill.getAddTransWaybill(JSON.stringify(waybillTr));
          } else {
            return false;
          }
        });
      }
    }
    $scope.$on('AddTransWaybill.Updated', function() {
      var result=AddTransWaybill.getResult();
      if (isButton==0){
        if (result.success==true){
          $ionicLoading.show({
            noBackdrop: true,
            template: "保存成功",
            duration: 1500
          });
          $state.go('tab.workbench');
        }
      }else if (isButton==1){
        waybillNum=result.rows.waybillNO;
        wayId=result.rows.wayId;
        destinationPhone=result.rows.deptPhone;
        if (result.success==true){
          $scope.popover.show(eventPrint);
          var printMsg={
            deptId:userMsg.rows.baseDeptId,
            userMsg:{
              username:loginMsg.username,
              password:loginMsg.password,
              uuid:loginMsg.uuid
            }
          };
          getPrinter.getPrintSet(JSON.stringify(printMsg));
        }
      }
    });
    $scope.saveTransWaybill=function(){
      isButton=0;
      addWaybill();
    };
    //打印
    $ionicPopover.fromTemplateUrl('templates/tab-workbench/addWaybill/printConfirm.html', {
      scope: $scope
    }).then(function(popover) {
      $scope.popover = popover;
    });
    //添加打印机
    $scope.scanBluetooth=function(){
      $cordovaHdoitBle.scan();
    };
    $scope.bluetooth='未连接蓝牙设备';
    var timer=window.setInterval(getBleName,1000);
    var waybillNum,destinationPhone,eventPrint,wayId;
    $scope.printTransWaybill=function($event){
      isButton=1;
      eventPrint=$event;
      addWaybill();
      $scope.$on('GetPrint.Updated', function() {
        $scope.printsMsg = getPrinter.getResult().rows;
        if ($scope.printsMsg.goods_flag==1){
          if ($scope.printsMsg.print_behave==1&&$scope.waybill.goodsInfo.PIECES!=''){
            $scope.printsMsg.goods_pieces=$scope.waybill.goodsInfo.PIECES;
            $('#goodsPieces').attr("readonly",true);
          }else if ($scope.printsMsg.print_behave==1&&$scope.waybill.goodsInfo.PIECES==''){
            $scope.printsMsg.goods_pieces=1;
            $('#goodsPieces').attr("readonly",true);
          }else if($scope.printsMsg.print_behave==0){
            $('#goodsPieces').focus();
          }
        }else {
          $('#goodsPieces').attr("readonly",true);
        }
        if ($scope.printsMsg.company_name==null){
          $scope.printsMsg.company_name='存根标题';
        }
        if ($scope.printsMsg.phone==null){
          $scope.printsMsg.phone='客服电话';
        }
        if ($scope.printsMsg.remark==null){
          $scope.printsMsg.remark='责任条文';
        }
      });
    };
    $scope.$on('$destroy', function() {
      $scope.popover.remove();
    });
    $scope.confirmPrint=function(){
      if ($scope.bluetooth=='未连接蓝牙设备'){
        $ionicLoading.show({
          noBackdrop: true,
          template: "请先连接打印机",
          duration: 1500
        });
      }else {
        if ($scope.printsMsg.company_flag==1){
          readyToPrintStub(0);
        }
        if ($scope.printsMsg.shipper_flag==1){
          readyToPrintStub(1);
        }
        if ($scope.printsMsg.goods_flag==1){
          for (var i=0;i<$scope.printsMsg.goods_pieces;i++){
            readyToPrintGoods();
          }
        }
        $scope.popover.remove();
        $ionicLoading.show({
          noBackdrop: true,
          template: "保存成功",
          duration: 1500
        });
        $state.go('tab.workbench');
      }
    };
    $scope.cancelPrint=function(){
      $scope.popover.remove();
      $ionicLoading.show({
        noBackdrop: true,
        template: "保存成功",
        duration: 1500
      });
      $state.go('tab.workbench');
    };
    function getBleName(){
      //蓝牙设备名称
      $cordovaHdoitBle.bluetoothName(function(success){
        if(success!=null){
          $scope.bluetooth=success+'已连接';
          $('#refresh').focus();
          window.clearInterval(timer);
        }
        else {
          $scope.bluetooth='未连接蓝牙设备';
        }
      },function(err){
        $scope.bluetooth='未连接蓝牙设备';
      });
    }
    function readyToPrintStub(i){
      var line;
      if ($("#site option:selected").val()!=''&&$scope.transWaybill.waybill.DESTINATION_STATION!=''){
        line=userMsg.rows.deptName+" → "+$("#site option:selected").text()+" → "+$scope.transWaybill.waybill.DESTINATION_STATION;
      }else if ($("#site option:selected").val()!=''&&$scope.transWaybill.waybill.DESTINATION_STATION==''){
        line=userMsg.rows.deptName+" → "+$("#site option:selected").text();
      }else if ($("#site option:selected").val()==''&&$scope.transWaybill.waybill.DESTINATION_STATION!=''){
        line=userMsg.rows.deptName+" → "+$scope.transWaybill.waybill.DESTINATION_STATION;
      }
      if ($scope.transWaybill.costMap.AGENCY_FUND==undefined||$scope.transWaybill.costMap.AGENCY_FUND==null){
        $scope.transWaybill.costMap.AGENCY_FUND='0.00';
      }
      if ($scope.transWaybill.costMap.PAY_COLLECTION==undefined||$scope.transWaybill.costMap.PAY_COLLECTION==null){
        $scope.transWaybill.costMap.PAY_COLLECTION='0.00';
      }
      if ($scope.transWaybill.costMap.FREIGHT_AMOUNT==undefined||$scope.transWaybill.costMap.FREIGHT_AMOUNT==null){
        $scope.transWaybill.costMap.FREIGHT_AMOUNT='0.00';
      }
      if ($scope.transWaybill.costMap.PAYMENT_FREIGHT==undefined||$scope.transWaybill.costMap.PAYMENT_FREIGHT==null){
        $scope.transWaybill.costMap.PAYMENT_FREIGHT='0.00';
      }
      if ($scope.transWaybill.paymentMap.BACK_NOW==undefined||$scope.transWaybill.paymentMap.BACK_NOW==null){
        $scope.transWaybill.paymentMap.BACK_NOW='0.00';
      }
      if ($scope.transWaybill.paymentMap.OWE_TO_RETURN==undefined||$scope.transWaybill.paymentMap.OWE_TO_RETURN==null){
        $scope.transWaybill.paymentMap.OWE_TO_RETURN='0.00';
      }
      //设置打印机页面
      var startPageOption = {"pageWidth":600,"pageHeight":400};
      $cordovaHdoitBle.startPageZhike(startPageOption);
      //打印文本
      var companyNmae = {"x":10,"y":5,"textSize":20,"fontName":"黑体","bold":true,"rotate":false};
      $cordovaHdoitBle.printTextZhike($scope.printsMsg.company_name,companyNmae);

      var waybillNO={"x":10,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike("运单号:",waybillNO);

      var waybillNOTxt={"x":110,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike(waybillNum,waybillNOTxt);

      var date={"x":330,"y":65,"textSize":2,"fontName":"宋体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike($scope.WAYBILL_DATE,date);

      var phone={"x":420,"y":17.5,"textSize":2,"fontName":"斜体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike($scope.printsMsg.phone,phone);

      if (i==0){
        var companyStub={"x":490,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
        $cordovaHdoitBle.printTextZhike("存根",companyStub);
      }else if(i==1){
        var shipperStub={"x":460,"y":65,"textSize":5,"fontName":"宋体","bold":false,"rotate":false};
        $cordovaHdoitBle.printTextZhike("发货方留存",shipperStub);
      }

      //线路及货物信息
      var lineAndGoods={"x":10,"y":105,"textSize":15,"fontName":"宋体","bold":true,"rotate":false};
      $cordovaHdoitBle.printTextZhike(line,lineAndGoods);

      //送货方式
      var deliveryType={"x":380,"y":145,"textSize":3,"fontName":"宋体","bold":true,"rotate":false};
      $cordovaHdoitBle.printTextZhike($scope.DELIVERY_TYPE,deliveryType);

      //收货人
      var consignee={"x":10,"y":145,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike("收货方",consignee);

      //收货人名称
      var consigneeName={"x":110,"y":145,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike($scope.transWaybill.customers.R_CUSTOMER_NAME.substring(0,4),consigneeName);

      //收货人电话
      var consigneePhone={"x":210,"y":145,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike($scope.transWaybill.customers.R_CUSTOMER_PHONE,consigneePhone);

      //发货人
      var shipper={"x":10,"y":175,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike("发货方",shipper);

      //发货人名称
      var shipperName={"x":110,"y":175,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike($scope.transWaybill.customers.S_CUSTOMER_NAME.substring(0,4),shipperName);

      //发货人电话
      var shipperPhone={"x":210,"y":175,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike($scope.transWaybill.customers.S_CUSTOMER_PHONE,shipperPhone);

      //代收、代收垫付
      var collection={"x":10,"y":215,"textSize":4,"fontName":"宋体","bold":true,"rotate":false};
      $cordovaHdoitBle.printTextZhike('代收:'+$scope.transWaybill.costMap.AGENCY_FUND+' '+'代收垫付:'+$scope.transWaybill.costMap.PAY_COLLECTION,collection);

      //运费、运费垫付
      var freight={"x":10,"y":245,"textSize":4,"fontName":"宋体","bold":true,"rotate":false};
      $cordovaHdoitBle.printTextZhike('运费:'+$scope.transWaybill.costMap.FREIGHT_AMOUNT+' '+'运费垫付:'+$scope.transWaybill.costMap.PAYMENT_FREIGHT,freight);

      //信息费
      var informationFee={"x":10,"y":285,"textSize":2,"fontName":"宋体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike('现返:'+$scope.transWaybill.paymentMap.BACK_NOW+' '+'欠返:'+$scope.transWaybill.paymentMap.OWE_TO_RETURN,informationFee);

      //到站信息
      if ($("#site option:selected").text()!=''){
        var destinationNote={"x":10,"y":315,"textSize":2,"fontName":"宋体","bold":false,"rotate":false};
        $cordovaHdoitBle.printTextZhike($("#site option:selected").text()+'  '+destinationPhone,destinationNote);
      }

      //备注
      var remark={"x":10,"y":345,"width":520,"height":64,"fontSize":2,"rotate":0,"bold":1,"underline":false,"reverse":false};
      $cordovaHdoitBle.multilineText($scope.printsMsg.remark,remark);

      //开票人
      var drawer={"x":380,"y":315,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike("开票人:"+userMsg.rows.cname,drawer);

      //扫码跟踪
      var tracking={"x":470,"y":215,"textSize":4,"fontName":"宋体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike("扫码跟踪",tracking);

      //打印二维码
      var QRCode = {"x":450,"y":90,"size":3};
      $cordovaHdoitBle.printQRcodeZhike("http://www.xyt56.net/html/scanning.html?wayId="+wayId,QRCode);
      $cordovaHdoitBle.doPrint();
      $cordovaHdoitBle.gotoMarklabel();
    }
    function readyToPrintGoods(){
      var line;
      if ($("#site option:selected").val()!=''&&$scope.transWaybill.waybill.DESTINATION_STATION!=''){
        line=userMsg.rows.deptName+" → "+$("#site option:selected").text()+" → "+$scope.transWaybill.waybill.DESTINATION_STATION;
      }else if ($("#site option:selected").val()!=''&&$scope.transWaybill.waybill.DESTINATION_STATION==''){
        line=userMsg.rows.deptName+" → "+$("#site option:selected").text();
      }else if ($("#site option:selected").val()==''&&$scope.transWaybill.waybill.DESTINATION_STATION!=''){
        line=userMsg.rows.deptName+" → "+$scope.transWaybill.waybill.DESTINATION_STATION;
      }
      //设置打印机页面
      var startPageOption = {"pageWidth":600,"pageHeight":430};
      $cordovaHdoitBle.startPageZhike(startPageOption);
      //打印文本
      var companyNmae = {"x":10,"y":-5,"textSize":20,"fontName":"黑体","bold":true,"rotate":false};
      $cordovaHdoitBle.printTextZhike($scope.printsMsg.company_name,companyNmae);

      var phone={"x":420,"y":7.5,"textSize":2,"fontName":"斜体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike($scope.printsMsg.phone,phone);

      //运单号
      var waybillNO={"x":10,"y":80,"textSize":20,"fontName":"宋体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike(waybillNum,waybillNO);

      //线路
      var lines={"x":10,"y":160,"textSize":20,"fontName":"黑体","bold":true,"rotate":false};
      $cordovaHdoitBle.printTextZhike(line,lines);

      //打印一维码
      var oneCode = {"x":10,"y":280,"type":'128','width':1,'height':85,'rotate':false};
      $cordovaHdoitBle.printBarcodeZhike(waybillNum,oneCode);

      var massage={"x":10,"y":380,"textSize":1,"fontName":"宋体","bold":false,"rotate":false};
      $cordovaHdoitBle.printTextZhike(waybillNum+'  '+$scope.WAYBILL_DATE,massage);

      $cordovaHdoitBle.doPrint();
      $cordovaHdoitBle.gotoMarklabel();
    }
  });
